package lion.netty4.message;

import io.netty.buffer.ByteBuf;
import io.netty.util.CharsetUtil;

public class RequestMessage {

	private int msgcode;

	private ByteBuf buffer;

	public RequestMessage(int msgcode, ByteBuf byteBuf) {
		buffer = byteBuf;
		this.msgcode = msgcode;
	}

	public int getMsgCode() {
		return msgcode;
	}
	
	public short readShort() {
		return buffer.readShort();
	}

	public int readInt() {
		return buffer.readInt();
	}

	public long readLong() {
		return buffer.readLong();
	}

	public byte readByte() {
		return buffer.readByte();
	}

	public short readUnsignedByte() {
		return buffer.readUnsignedByte();
	}

	public int readUnsignedShort() {
		return buffer.readUnsignedShort();
	}

	public long readUnsignedInt() {
		return buffer.readUnsignedInt();
	}

	public ByteBuf readByteBuf() {
		int utflen = buffer.readUnsignedShort();
		return buffer.readBytes(utflen);
	}

	public String readString() {
		int utflen = buffer.readUnsignedShort();
		if (utflen == 0) {
			return "";
		}
		return buffer.readBytes(utflen).toString(CharsetUtil.UTF_8);
	}

	public boolean readBool() {
		return buffer.readBoolean();
	}
	
	public void releaseBuffer(){
		buffer.release();
	}

	public byte[] readBytes() {
		int msglen = buffer.readUnsignedShort();
		return buffer.readBytes(msglen).array();
	}

}
